﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProductKeyManager.DB;
using System.Xml.Linq;

namespace ProductKeyManager.Xml
{
    /// <summary>
    /// XAML hanlder
    /// </summary>
    public class XamlHandler : IXmlHandler
    {
        #region IXmlHandler Members

        private IEnumerable<Product> _Products;

        /// <summary>
        /// Gets the products imported from the XML.
        /// </summary>
        /// <value>The products.</value>
        public IEnumerable<Product> Products
        {
            get { return _Products; }
        }

        /// <summary>
        /// Imports the XML.
        /// </summary>
        /// <param name="fileUri">The file URI.</param>
        public void ImportXml(Uri fileUri)
        {
            _Products = null;
            throw new NotImplementedException();
        }

        private string _GeneratedXml;

        /// <summary>
        /// Gets the XML generated by from the products.
        /// </summary>
        /// <value>The generated XML.</value>
        public string GeneratedXml
        {
            get { return _GeneratedXml; }
        }

        /// <summary>
        /// Generates the XML.
        /// </summary>
        /// <param name="products">The products.</param>
        public void GenerateXml(IEnumerable<Product> products)
        {
            GenerateXml(products, false, string.Empty);
        }

        /// <summary>
        /// Generates the XML.
        /// </summary>
        /// <param name="products">The products.</param>
        /// <param name="generateStyleSheet">if set to <c>true</c> the stylesheet is generated.</param>
        /// <param name="fileName">Name of the file.</param>
        public void GenerateXml(IEnumerable<Product> products, bool generateStyleSheet, string fileName)
        {
            XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
            string xmlResult = String.Empty;
            XElement xml = new XElement(ns + "Page",
                new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
                new XElement(ns + "Page.Resources",
                PanelBackgroundStyle(ns),
                KeyAllocatedLabelStyles(ns)),
                new XElement(ns + "ScrollViewer",
                    new XElement(ns + "StackPanel",
                    from p in products
                    select new XElement(ns + "DockPanel",
                        new XElement(ns + "StackPanel",
                            new XAttribute("Orientation", "Horizontal"),
                            new XAttribute("Style", "{StaticResource PanelBackground}"),
                            new XAttribute("DockPanel.Dock", "Top"),
                            new XElement(ns + "TextBlock",
                                new XAttribute("FontWeight", "SemiBold"),
                                new XAttribute("Margin", "4"),
                                new XAttribute("Text", p.Name))),
                        new XElement(ns + "StackPanel",
                            new XAttribute("Orientation", "Horizontal"),
                            new XAttribute("DockPanel.Dock", "Bottom"),
                            new XElement(ns + "TextBlock",
                                new XAttribute("FontWeight", "SemiBold"),
                                new XAttribute("Margin", "4"),
                                new XAttribute("Text", "Notes: ")),
                            new XElement(ns + "TextBlock",
                                new XAttribute("Margin", "4"),
                                new XAttribute("Text", p.Notes))),
                        new XElement(ns + "ScrollViewer",
                            new XAttribute("VerticalScrollBarVisibility", "Auto"),
                            new XAttribute("MaxHeight", "150"),
                            new XElement(ns + "StackPanel",
                                from k in p.Keys
                                select new XElement(ns + "StackPanel",
                                    new XAttribute("Orientation", "Horizontal"),
                                    new XElement(ns + "Label",
                                        new XAttribute("Style", "{StaticResource KeyAllocToolBarLabel}"),
                                        new XAttribute("Content", "Key: ")),
                                    new XElement(ns + "Label",
                                        new XAttribute("Content", k.Code)),
                                    new XElement(ns + "Label",
                                        new XAttribute("Style", "{StaticResource KeyAllocToolBarLabel}"),
                                        new XAttribute("Content", "Allocated To: ")),
                                    new XElement(ns + "Label",
                                        new XAttribute("Content", k.AllocatedTo)))))))));

            _GeneratedXml = xmlResult + xml.ToString();
        }

        #endregion

        private static XElement PanelBackgroundStyle(XNamespace ns)
        {
            XElement element = new XElement(ns + "Style",
                new XAttribute("{http://schemas.microsoft.com/winfx/2006/xaml}Key", "PanelBackground"),
                new XAttribute("TargetType", "{x:Type Panel}"),
                   new XElement(ns + "Setter",
                    new XAttribute("Property", "Background"),
                    new XElement(ns + "Setter.Value",
                        new XElement(ns + "LinearGradientBrush",
                            new XAttribute("StartPoint", "0,0.5"),
                            new XAttribute("EndPoint", "1,0.5"),
                            new XElement(ns + "LinearGradientBrush.RelativeTransform",
                                new XElement(ns + "TransformGroup",
                                    new XElement(ns + "TranslateTransform",
                                        new XAttribute("X", "-0.5"),
                                        new XAttribute("Y", "-0.5")
                                    ),
                                    new XElement(ns + "ScaleTransform",
                                        new XAttribute("ScaleX", "1"),
                                        new XAttribute("ScaleY", "1")
                                    ),
                                    new XElement(ns + "SkewTransform",
                                        new XAttribute("AngleX", "0"),
                                        new XAttribute("AngleY", "0")
                                    ),
                                    new XElement(ns + "RotateTransform",
                                        new XAttribute("Angle", "90.0172795589426")
                                    ),
                                    new XElement(ns + "TranslateTransform",
                                        new XAttribute("X", "0.5"),
                                        new XAttribute("Y", "0.5")
                                    ),
                                    new XElement(ns + "TranslateTransform",
                                        new XAttribute("X", "-0.0015136743804197244"),
                                        new XAttribute("Y", "-0.19736842105263153")
                                    )
                                )
                            ),
                            new XElement(ns + "LinearGradientBrush.GradientStops",
                                new XElement(ns + "GradientStop",
                                    new XAttribute("Color", "Silver"),
                                    new XAttribute("Offset", "0.0")
                                ),
                                new XElement(ns + "GradientStop",
                                    new XAttribute("Color", "White"),
                                    new XAttribute("Offset", "1.5")
                                )
                            )
                        ))
                    )
                );
            return element;
        }
        private static XElement KeyAllocatedLabelStyles(XNamespace ns)
        {
            XElement element = new XElement(ns + "Style",
                new XAttribute("{http://schemas.microsoft.com/winfx/2006/xaml}Key", "KeyAllocToolBarLabel"),
                new XAttribute("TargetType", "{x:Type Label}"),
                    new XElement(ns + "Setter",
                        new XAttribute("Property", "FontWeight"),
                        new XAttribute("Value", "SemiBold")
                       )
                );
            return element;
        }
    }
}
